package com.zy.server.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.json.JSONObject;

import java.util.Objects;

public class AliSmsUtil {
    public static boolean sendSmsCode(String phone, int code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4GJ2ji7sXgkzPD1cKrzV", "rglxxOmewZJyuUjeuoRMpuKxdAneps");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "鑫哥的呵护");
        request.putQueryParameter("TemplateCode", "SMS_205875996");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            if (response.getHttpStatus() == 200) {
                JSONObject jsonObject=new JSONObject(response.getData());

                return Objects.equals(jsonObject.getString("Code"),"OK");

            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
